<?php
if (!defined('IN_DISCUZ')){
	exit('Access Denied');
}

global $_G;

//////////////////////////////////////////////KIEM TRA DIEU KIEN GIAN HANG//////////////////////////////////////////
if (!$_G['uid']){
	showmessage('postperm_login_nopermission', NULL, array(), array(
			'login'=>1
	));
}

if (!$uData)
	showmessage('ban_chua_dang_ky_gian_hang', 'shop.php?mod=store&ac=register');

if ($uData['verificationed'] != 1)
	showmessage('you_not_complete_profile_to_post', 'shop.php?mod=store&ac=profile&do=update');

// Kiem tra categories da dang ky chua
if ($uData['sid'] == 0){
	showmessage('ban_chua_dang_ky_dang_muc_ban_hang', 'shop.php?mod=store&ac=profile&do=update');
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$submited = submitcheck('submit', 1, $seccodecheck);

// Step 1
if (!$sid){
	$category_num = 1;
	$shoplist = shop_menu($uData['sid']);
}else{ // Step 2
	// Danh muc ko cho phep them moi/sua san pham
	if ($_G['shop']['alloweditpost'] == 0)
		showmessage('post_shop_newproduct_nopermission', NULL);

	// Dang muc dang tam an (display: none)
	if ($_G['shop']['status'] == 0)
		showmessage('shop_category_hiden', NULL);

	/*
	 * Ma san pham
	 * Tu dong tao ma san pham theo user_name
	 * */
	$product['pcode'] = shopcode($_G['username']) . '-' . createRandomCode();

	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	if ($submited){

		// Categories ID
		$product['cid'] = isset($_G['gp_cid']) ? intval($_G['gp_cid']) : 0;

		// UID
		$product['uid'] = isset($_G['gp_uid']) ? intval($_G['gp_uid']) : 0;
		$product['username'] = $_G['username'];

		$product['pname'] = isset($_G['gp_pname']) ? get_str_nohtml($_G['gp_pname']) : '';
		if (strlen($product['pname']) < 6 || strlen($product['pname']) > 80)
			$post_erro['pname'] = 1;

		$gp['pprice'] = isset($_G['gp_pprice']) ? get_str_nohtml($_G['gp_pprice']) : '';
		$product['pprice'] = str_replace('.', '', $gp['pprice']);
		$product['pprice'] = intval($product['pprice']);
		if ($product['pprice'] < 2000)
			$post_erro['pprice'] = 1;

		$product['quantity'] = isset($_G['gp_quantity']) ? intval($_G['gp_quantity']) : 0;
		if ($product['quantity'] < 1)
			$post_erro['quantity'] = 1;

		$gp['delivery_price'] = isset($_G['gp_delivery_price']) ? get_str_nohtml($_G['gp_delivery_price']) : '';
		$product['delivery_price'] = str_replace('.', '', $gp['delivery_price']);
		$product['delivery_price'] = intval($product['delivery_price']);
		if ($product['delivery_price'] < 0)
			$product['delivery_price'] = 0;

		$product['punit'] = isset($_G['gp_unit']) ? get_str_nohtml($_G['gp_unit']) : '';

		$product['state'] = isset($_G['gp_state']) ? intval($_G['gp_state']) : 100;
		if ($product['state'] == 0)
			$post_erro['state'] = 1;

		/*
		 * Discount
		 * */
		$product['sales']['rate'] = isset($_G['gp_sales']['rate']) ? intval($_G['gp_sales']['rate']) : 0;
		$product['sales'] = $product['sales']['rate'] <= 0 ? 0 : ($product['sales']['rate'] > 100 ? 100 : $product['sales']['rate']);

		if ($product['sales'] > 0){
			$gp['sales']['from'] = isset($_G['gp_sales']['from']) ? get_str_nohtml($_G['gp_sales']['from']) : '';
			if (empty($gp['sales']['from']))
				$post_erro['sales']['from'] = 1;

			$product['b_sales'] = strtotime($gp['sales']['from']);

			$gp['sales']['end'] = isset($_G['gp_sales']['end']) ? get_str_nohtml($_G['gp_sales']['end']) : '';
			$product['e_sales'] = strtotime($gp['sales']['end']);
		}

		/*
		 * Thue cua san pham
		 * */
		$gp['tax']['used'] = isset($_G['gp_tax']['used']) ? intval($_G['gp_tax']['used']) : 0;
		if ($gp['tax']['used'] == 1){
			$gp['tax']['tax_vat'] = isset($_G['gp_tax']['tax_vat']) ? intval($_G['gp_tax']['tax_vat']) : 0;
			if ($gp['tax']['tax_vat'] == 0){
				$gp['tax']['tax_vat'] = 10;
				$post_erro['tax']['tax_vat'] = 1;
			}

			$gp['tax']['tax_import'] = isset($_G['gp_tax']['tax_import']) ? intval($_G['gp_tax']['tax_import']) : 0;
			$gp['tax']['tax_other'] = isset($_G['gp_tax']['tax_other']) ? intval($_G['gp_tax']['tax_other']) : 0;
		}
		if (!empty($gp['tax'])){
			$product['tax'] = serialize($gp['tax']);
		}

		$product['status'] = isset($_G['gp_status']) ? intval($_G['gp_status']) : 1;
		$product['location_province'] = isset($_G['gp_location_province']) ? intval($_G['gp_location_province']) : 0;

		$product['summary'] = isset($_G['gp_summary']) ? get_str_nohtml($_G['gp_summary']) : '';
		if (strlen($product['summary']) < 20 || strlen($product['summary']) > 300)
			$post_erro['summary'] = 1;

	// Image
		$product['img_cover'] = isset($_G['gp_imgdefault']) ? get_str_nohtml($_G['gp_imgdefault']) : '';
		if (!$product['img_cover'])
			$post_erro['img_cover'] = 1;

		$gp['images'] = isset($_G['gp_images']) ? $_G['gp_images'] : '';
		$product['images'] = serialize($gp['images']);

		$gp['extra'] = isset($_G['gp_extra']) ? get_str_nohtml($_G['gp_extra']) : '';

		for ($i = 0; $i < $_G['shop']['param_number']; $i++){
			if (!empty($gp['extra']['custom_' . $i])){
				$product['custom_' . $i] = addslashes(serialize($gp['extra']['custom_' . $i]));
			}
		}

		$editor['value'] = $product['detail'] = isset($_G['gp_detail']) ? censor(trim($_G['gp_detail'])) : '';

		/*
		 * Check error exists
		 * if empty then submit
		 * */
		if (empty($post_erro) && $product['cid'] == $sid && $product['uid'] == $_G['uid']){
			if ($pid = insert_product($product)){
				// move image upload to /attach_dir/
				if (count($gp['images']) > 0){
					$imglistattach = array();

					foreach ($gp['images'] as $mid){
						if ($attach = DB::fetch(DB::query("SELECT * FROM " . DB::table("forum_attachment_unused") . " WHERE aid = " . $mid))){
							$imglistattach[$attach['aid']] = $attach;
						}
					}

					move_attach($imglistattach, $pid, 'insert', true);
					DB::update("shop_products", array(
							'available'=>TRUE
					), array(
							'pid'=>$pid
					));
				}

				// Add feed
				require libfile('function/post');

				$pname = locdau($product['pname']);
				$feed = array(
						'icon'=>'',
						'title_template'=>'',
						'title_data'=>array(),
						'body_template'=>'',
						'body_data'=>array(),
						'title_data'=>array(),
						'images'=>array()
				);
				$feed['icon'] = 'product';
				$feed['title_template'] = 'feed_product_title';
				$feed['body_template'] = 'feed_product_message';
				$feed['body_data'] = array(
						'subject'=>'<a href="shop.php?mod=view&name=' . $pname . '&pid=' . $pid . '">' . $product['pname'] . '</a>',
						'message'=>$product['summary'] . lang('shop/template', 'feed_gia_ban') . FormatNumber($product['pprice'])
				);
				$feed['images'] = array(
						'http://' . $_SERVER['HTTP_HOST'] . '/' . getattachURL($imglistattach[$product['img_cover']], 'shop/')
				);
				$feed['image_links'] = array(
						"shop.php?mod=view&name=$pname&pid=$pid"
				);
				$feed['title_data']['hash_data'] = "product{$pid}";
				$feed['id'] = $pid;
				$feed['idtype'] = 'product';

				// Post feed to wall
				postfeed($feed);
				/*
				 * Send notice to all friends
				 * */
				$listFriend = myfriend($_G['uid']);
				$listFriend = array_unique($listFriend);
				$update_array = array(
						'actor'=>$_G['username'],
						'url'=>'shop.php?mod=view&name=' . $pname . '&pid=' . $pid,
						'pname'=>$product['pname']
				);
				foreach ($listFriend as $key=>$touid){
					notification_add($touid, 'newproduct', 'add_new_product', $update_array, 1);
				}

				dsetcookie('shop', '');
				showmessage('post_product_is_succeed', 'shop.php?mod=view&pid=' . $pid);
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
include template('shop/store/new_product');